package com.whj.servicedriveruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whj.internalcommon.constant.CommonStatusEnum;
import com.whj.internalcommon.constant.DriverCarConstants;
import com.whj.internalcommon.dto.*;
import com.whj.internalcommon.response.OrderDriverResponse;
import com.whj.servicedriveruser.mapper.CarMapper;
import com.whj.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.whj.servicedriveruser.mapper.DriverUserMapper;
import com.whj.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import com.whj.servicedriveruser.service.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wanghaijun
 * @Date: 2023/1/11 - 01 - 11 - 15:40
 * @Description: com.whj.servicedriveruser.service.impl
 */
@Service
public class DriverUserServiceImpl implements DriverUserService {
    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Autowired
    private CarMapper carMapper;

    @Override
    public ResponseResult testGetDriverUser() {
        DriverUser driverUser = driverUserMapper.selectById(1);
        System.out.println("driverUser = " + driverUser);
        return ResponseResult.success();
    }

    /**
     * 添加司机用户，并初始化状态
     *
     * @param driverUser
     * @return
     */
    @Override
    public ResponseResult addDriverUser(DriverUser driverUser) {

        LocalDateTime now = LocalDateTime.now();
        driverUser.setUpdateTime(now);
        driverUser.setCreateTime(now);

        driverUserMapper.insert(driverUser);

        //初始化司机工作状态
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setUpdateTime(now);
        driverUserWorkStatus.setCreateTime(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);

        return ResponseResult.success("");
    }

    /**
     * 根据用户id修改信息
     *
     * @param driverUser
     * @return
     */
    @Override
    public ResponseResult updateDriverUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setUpdateTime(now);
        driverUserMapper.updateById(driverUser);
        return ResponseResult.success();
    }

    /**
     * 根据手机号查寻司机用户是否存在
     * @param driverPhone 查询的手机号
     * @return
     */
    @Override
    public ResponseResult <DriverUser> getDriverUserByPhone(String driverPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("driver_phone",driverPhone);
        map.put("state", DriverCarConstants.DRIVER_STATE_VALID);
        List<DriverUser> driverUsers = driverUserMapper.selectByMap(map);
        if(driverUsers.isEmpty()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_NOT_EXISTS.getValue());
        }
        DriverUser driverUser = driverUsers.get(0);
        return ResponseResult.success(driverUser );
    }

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;


    /**
     * 根据车辆ID查询订单所需要的信息
     *
     * @param carId
     * @return
     */
    @Override
    public ResponseResult<OrderDriverResponse> getAvailableDriver(Long carId) {

        //根据carId 和 bindState 查询车辆的绑定关系获取车辆ID
        QueryWrapper<DriverCarBindingRelationship> DriverCarBindingRelationshipQueryWrapper = new QueryWrapper<>();
        DriverCarBindingRelationshipQueryWrapper.eq("car_id",carId);
        DriverCarBindingRelationshipQueryWrapper.eq("bind_state",DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(DriverCarBindingRelationshipQueryWrapper);
        Long driverId = driverCarBindingRelationship.getDriverId();

        // 根据车辆ID 获取司机的工作状态 并获得司机ID
        QueryWrapper<DriverUserWorkStatus> DriverUserWorkStatusQueryWrapper = new QueryWrapper<>();
        DriverUserWorkStatusQueryWrapper.eq("driver_id",driverId);
        DriverUserWorkStatusQueryWrapper.eq("work_status",DriverCarConstants.DRIVER_WORK_STATUS_START);
        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(DriverUserWorkStatusQueryWrapper);
        if(null == driverUserWorkStatus){
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(),CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
        }else {

            // 根据司机ID 获取司机信息
            QueryWrapper<DriverUser> DriverUserQueryWrapper = new QueryWrapper<>();
            DriverUserQueryWrapper.eq("id",driverId);
            DriverUser driverUser = driverUserMapper.selectOne(DriverUserQueryWrapper);

            // 获取车辆信息
            QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
            carQueryWrapper.eq("id",carId);
            Car car = carMapper.selectOne(carQueryWrapper);


            OrderDriverResponse orderDriverResponse = new OrderDriverResponse();
            orderDriverResponse.setCarId(carId);
            orderDriverResponse.setDriverId(driverId);
            String driverPhone = driverUser.getDriverPhone();
            orderDriverResponse.setDriverPhone(driverPhone);

            orderDriverResponse.setLicenseId(driverUser.getLicenseId());
            orderDriverResponse.setVehicleNo(car.getVehicleNo());
            orderDriverResponse.setVehicleType(car.getVehicleType());

            return ResponseResult.success(orderDriverResponse);
        }
    }
}
